
extends KinematicBody2D

# member variables here, example:
# var a=2
# var b="textvar"

func _ready():
	set_fixed_process(true)
	
const GRAVITY = 800
const WALK_SPEED = 200
const JUMP_VELOCITY = -700

var velocity = Vector2()
var animating = false
var jumping = false
var jumpheld = false
var stop = false

func stop():
	print("Stop called!")
	stop = true

func _fixed_process(delta):

	velocity.y += delta * GRAVITY

	if(stop == false):

    	if (Input.is_action_pressed("move_left")):
	        velocity.x = - WALK_SPEED
	        get_node("sprite").set_flip_h(true)
	
	        if (animating == false):
	            get_node("./animation").play("run")
	            animating = true
	    elif (Input.is_action_pressed("move_right")):
	        velocity.x =  WALK_SPEED
	        get_node("sprite").set_flip_h(false)
	
	        if (animating == false):
	             get_node("./animation").play("run")
	             animating = true
	    else:
	        get_node("./animation").play("idle")
	        animating = false
	        velocity.x = 0
	
	    if (Input.is_action_pressed("jump")):
	        if(jumping == false):
	            velocity.y = JUMP_VELOCITY
	            jumping = true
	    
	if(stop == true):
		velocity.y = 0
		stop = false

	var motion = velocity * delta
	motion = move( motion ) 

    # FIXME - This stops the player when they're standing on the ground
	if (is_colliding()):
		var n = get_collision_normal()

		if(get_collider().get_name() == "breakable_brick"):
			if(n.x == 0 && n.y == 1):
				print("Collided with a brick!")
				get_collider().queue_free()
				var mini_brick_scene = load("res://breakable_brick.scn")
				var mini_brick_node = mini_brick_scene.instance()
#				var pos = get_pos()
#				pos.x = 5
#				pos.y = -5
#				mini_brick_node.set_pos(pos)
				add_child(mini_brick_node)
     
        
		print(n)
		motion = n.slide( motion ) 
		velocity = n.slide( velocity )
		move( motion )
            
        
        # FIXME - this is swag
		if((velocity.y < 4) and (velocity.y > -4)):
			jumping = false
